package com.baomidou.springwind.controller;

import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.entity.Department;
import com.baomidou.springwind.entity.Employee;
import com.baomidou.springwind.service.IDepartmentService;
import com.baomidou.springwind.service.IEmployeeService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2016/12/13.
 */

@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IDepartmentService iDepartmentService;


    @Permission("5001")
    @RequestMapping("/list")
    public String list(Model model) {
        return "/employee/list";
    }

    @ResponseBody
    @Permission("5001")
    @RequestMapping("/getEmployeeList")
    public String getEmployeeList() {
        Page<Employee> page = iEmployeeService.selectAllEmployee(this.<Employee>getPage());
//        Employee employee = new Employee();
//        employee.setId("总计");
//        int basicwage = 0,bons = 0,allowance = 0;
//        for (Employee e : list) {
//            basicwage += e.getBasicwage();
//            bons += e.getBonus();
//            allowance += e.getAllowance();
//        }
//        employee.setBasicwage(basicwage);
//        employee.setBonus(bons);
//        employee.setAllowance(allowance);
//        list.add(employee);
        page.setRecords(total(page.getRecords()));
        return jsonPage(page);
    }

    protected List<Employee> total(List<Employee> list) {
        Employee employee = new Employee();
        employee.setId("总计");
        int basicwage = 0, bons = 0, allowance = 0;
        for (Employee e : list) {
            basicwage += e.getBasicwage();
            bons += e.getBonus();
            allowance += e.getAllowance();
        }
        employee.setBasicwage(basicwage);
        employee.setBonus(bons);
        employee.setAllowance(allowance);
        list.add(employee);
        return list;
    }

    @Permission("5001")
    @RequestMapping("/edit")
    public String edit(Model model, String id) {
        if (id != null) {
            Employee employee = iEmployeeService.selectEmployeeById(id).get(0);
            model.addAttribute("employee", employee);
            List<Department> departments = iDepartmentService.selectList(new EntityWrapper<Department>());
            model.addAttribute("departmentList", departments);
        }
        return "/employee/edit";
    }

    @ResponseBody
    @Permission("5001")
    @RequestMapping("/updateEmployee")
    public String upDateEmployee(Employee employee) {
        boolean rtl = false;
        if (employee.getId() != null && !"".equals(employee.getId())) {
            rtl = iEmployeeService.upDateEmployeeById(employee);
        } else {
            Date date = new Date();
            String dateString = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
            employee.setId(DateFormatUtils.format(date, "yyyyMMddHHmmss"));
            employee.setDatastatus(1);
            employee.setCreatetime(dateString);
            employee.setChangetime(dateString);
            rtl = iEmployeeService.insert(employee);
        }
        return callbackSuccess(rtl);
    }

    @ResponseBody
    @Permission("5001")
    @RequestMapping("/deleteEmployee/{id}")
    public String deleteEmployeeById(@PathVariable String id) {
        iEmployeeService.deleteEmployeeById(id);
        return Boolean.TRUE.toString();
    }

    @RequestMapping("/Attendance")
    @Permission("7001")
    public String attendance(Model model, String id) {
        Employee employee = iEmployeeService.selectEmployeeById(id).get(0);
        model.addAttribute("employee", employee);
        return "/employee/attendance";
    }

    @ResponseBody
    @RequestMapping("/seach/{key}_{keyword}")
    @Permission("5001")
    public String seach(@PathVariable("key") String key, @PathVariable("keyword") String keyword) {
        Page<Employee> page = this.getPage();
        try {
            keyword = URLDecoder.decode(keyword, "UTF-8");
            if (key.equals("num")) {
                page = iEmployeeService.selectByEmployeeName(this.<Employee>getPage(), keyword);
            } else if (key.equals("name")) {
                page = iEmployeeService.selectByEmployeeNum(page, keyword);
            } else if (key.equals("id")) {
                page.setRecords(iEmployeeService.selectEmployeeById(keyword));
            } else if (key.equals("departmentname")) {
                page = iEmployeeService.selectByDepartmentName(page, keyword);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        page.setRecords(total(page.getRecords()));
        return jsonPage(page);
    }


}
